/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormVenda.java
 *
 * Created on 01/09/2011, 00:18:19
 */
package javalee.forms;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javalee.util.Uteis;
import javalee.dao.ItemVendaDAO;
import javalee.dao.VendaDAO;
import javalee.maps.Cliente;
import javalee.maps.ItensVenda;
import javalee.maps.Venda;
import javalee.maps.objetoImpressao;
import javalee.maps.produtosNota;
import javalee.util.GeraNotaPDF;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;
import sun.applet.resources.MsgAppletViewer;

/**
 *
 * @author Thiago
 */
public class FormVenda extends javax.swing.JFrame {

    private VendaDAO vDAO;
    private Cliente Cli;
    private formListaCliente frmCli;
    private FormItensVenda frmItens;
    /** Creates new form FormVenda */
    public FormVenda() {
        initComponents();
        vDAO = new VendaDAO();
        Cli = new Cliente();
        frmCli = new formListaCliente(Cli);
        frmItens = new FormItensVenda();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCli = new javax.swing.JButton();
        btnitens = new javax.swing.JButton();
        txtid = new javax.swing.JTextField();
        btnSalvar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbVenda = new javax.swing.JTable();
        btnNovo = new javax.swing.JButton();
        txtConsulta = new javax.swing.JTextField();
        btnExcluir = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtCli = new javax.swing.JTextField();
        javax.swing.text.MaskFormatter mf = null;
        try {
            mf = new javax.swing.text.MaskFormatter("##/##/####");
        } catch (java.text.ParseException p) {
            p.printStackTrace();
        }
        txtData = new javax.swing.JFormattedTextField(mf);
        jLabel6 = new javax.swing.JLabel();
        javax.swing.text.MaskFormatter mf2 = null;
        try {
            mf2 = new javax.swing.text.MaskFormatter("##/##/####");
        } catch (java.text.ParseException p) {
            p.printStackTrace();
        }
        txtDataEntrega = new javax.swing.JFormattedTextField(mf2);
        jLabel2 = new javax.swing.JLabel();
        txtValor = new javax.swing.JTextField();
        btnNota = new javax.swing.JButton();

        setTitle("Formulario de Venda");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        btnCli.setText("Cliente");
        btnCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCliActionPerformed(evt);
            }
        });

        btnitens.setBackground(new java.awt.Color(153, 255, 153));
        btnitens.setText("Itens");
        btnitens.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnitensActionPerformed(evt);
            }
        });

        txtid.setForeground(new java.awt.Color(255, 0, 0));
        txtid.setCaretColor(new java.awt.Color(255, 3, 0));
        txtid.setEnabled(false);
        txtid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtidActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        jLabel4.setText("ID : ");

        btnClose.setText("Fechar");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        tbVenda.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID Venda", "Cliente", "Data", "Data de Entrega", "Preço"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbVenda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tbVendaMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tbVenda);

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        txtConsulta.setName(""); // NOI18N
        txtConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtConsultaKeyReleased(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        jLabel5.setText("Data :");

        jLabel1.setText("Cliente :");

        txtCli.setEditable(false);
        txtCli.setEnabled(false);

        txtData.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(mf,mf));
        txtData.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDataFocusLost(evt);
            }
        });

        jLabel6.setText("Data Entrega:");

        txtDataEntrega.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(mf2,mf2));
        txtDataEntrega.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDataEntregaFocusLost(evt);
            }
        });

        jLabel2.setText("Valor:");

        txtValor.setEditable(false);
        txtValor.setName("txtValor"); // NOI18N

        btnNota.setBackground(new java.awt.Color(255, 255, 102));
        btnNota.setText("Nota Fiscal");
        btnNota.setToolTipText("");
        btnNota.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNotaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtConsulta)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addGap(51, 51, 51)
                                                .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(btnitens))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(btnNota)
                                                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                        .addGap(53, 53, 53)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel5)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtDataEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btnCli, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtCli, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnNovo)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnExcluir)
                                        .addGap(40, 40, 40)
                                        .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addComponent(jLabel4)))
                        .addGap(76, 76, 76)
                        .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtConsulta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtDataEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnitens))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(btnCli)
                    .addComponent(jLabel1)
                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(59, 59, 59)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNovo)
                    .addComponent(btnExcluir)
                    .addComponent(btnSalvar)
                    .addComponent(btnClose)
                    .addComponent(btnNota))
                .addContainerGap())
        );

        txtConsulta.getAccessibleContext().setAccessibleName("");
        txtConsulta.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCliActionPerformed
    frmCli.setVisible(true);
}//GEN-LAST:event_btnCliActionPerformed

private boolean ValidaData(){
        String d = txtData.getText();
        boolean retorno = true;
        if(!d.equals("  /  /    ")){
            Pattern p = Pattern.compile("^(?:(?:31(\\/|-|\\.)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$"); 
            Matcher m = p.matcher(d);  
            if (!m.find()){
                JOptionPane.showMessageDialog(null, "Data inválida.");
                retorno = false;
                txtData.setText("");
            }  
        }
        else{
            JOptionPane.showMessageDialog(null, "Data vazia. Preencha a data.");
            retorno = false;
        }
        return retorno;
}

private void btnitensActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnitensActionPerformed
    int row = tbVenda.getSelectedRow();
    if (row != -1){
    DefaultTableModel tableModel = (DefaultTableModel)tbVenda.getModel();
    
    frmItens.setIdVenda(Integer.parseInt(tableModel.getValueAt(row, 0).toString()));
    frmItens.setVisible(true);
    }
}//GEN-LAST:event_btnitensActionPerformed

private void txtidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtidActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtidActionPerformed

private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
    if( "".equals(txtCli.getText())){
        JOptionPane.showMessageDialog(null, "Selecione um cliente.");
    }
    else if(!ValidaData()){
       
    }
    else{
    try{
        
     int row = tbVenda.getSelectedRow();
        Venda v = new Venda();
        v.setCliente(Cli);
        
        int dia = Integer.parseInt(txtData.getText().substring(0, 2));
        int mes = Integer.parseInt(txtData.getText().substring(3, 5));
        int ano = Integer.parseInt(txtData.getText().substring(6, 10));
        
        v.setData(ano+"-"+mes+"-"+dia);
         
        int diae = Integer.parseInt(txtDataEntrega.getText().substring(0, 2));
        int mese = Integer.parseInt(txtDataEntrega.getText().substring(3, 5));
        int anoe = Integer.parseInt(txtDataEntrega.getText().substring(6, 10));
        
        v.setDataEntrega(anoe+"-"+mese+"-"+diae);        
        
        v.setValor(0);
        
         if(row==-1){      
            if (vDAO.cadastrar(v)) {
                JOptionPane.showMessageDialog(null, "Venda cadastrada.");
                frmItens.setIdVenda(vDAO.getUltimoID());
                frmItens.setVisible(true);
                //txtMaquina.setText("");
            } else {
                JOptionPane.showMessageDialog(null, "Problema ao cadastrar Venda.");
            }
        }else{
         JOptionPane.showMessageDialog(null, "Clique em 'Novo' antes de cadastrar uma venda.");
        }
}
 
     catch (Exception e) {
        System.out.println(e.toString());
    }
 preencheDados();
 //limpaDados();
    }
}//GEN-LAST:event_btnSalvarActionPerformed

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
dispose();
}//GEN-LAST:event_btnCloseActionPerformed

private void preencheDados(){
    if(!Uteis.validaCampoTexto(txtConsulta)){
            JOptionPane.showMessageDialog(null,"Campo de pesquisa está incorreto.");
        }
        else{
     //Trago a lista de maquinas do banco
     ArrayList<Venda> lista = vDAO.consultar(txtConsulta.getText().toString());
     tbVenda.removeAll();
     
     //Pego o TableModel do componente JTable
     DefaultTableModel tableModel = (DefaultTableModel)tbVenda.getModel();  
     //Limpo os dados do componente
     tableModel.setNumRows(0);
     
     //Passo pela lista de Maquinas, inserindo no JTable
     for(Iterator<Venda> it = lista.iterator(); it.hasNext();){
        Venda v = (Venda)it.next();
        tableModel.addRow(new Object[]{
            v.getId(),//Cada campo que coloco vira uma coluna
            v.getCliente().getNome(),
            v.getData(),
            v.getDataEntrega(),
            v.getValor()
        });     
    }   
    }
}
private void tbVendaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbVendaMouseReleased
    int row = tbVenda.getSelectedRow();
    DefaultTableModel tableModel = (DefaultTableModel)tbVenda.getModel();  
    
    txtid.setText(tableModel.getValueAt(row, 0).toString());
    Cli.setNome(tableModel.getValueAt(row, 1).toString());
    txtCli.setText(Cli.getNome());
    
    txtData.setText(tableModel.getValueAt(row, 2).toString());
    txtDataEntrega.setText(tableModel.getValueAt(row, 3).toString());
    
    txtValor.setText(tableModel.getValueAt(row, 4).toString());
    
    VendaEstaSelecionada(true);
}//GEN-LAST:event_tbVendaMouseReleased

private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
limpaDados();
tbVenda.clearSelection();
VendaEstaSelecionada(false);
}//GEN-LAST:event_btnNovoActionPerformed

private void txtConsultaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtConsultaKeyReleased
    if(evt.getKeyCode()==10){
        preencheDados();
    }
}//GEN-LAST:event_txtConsultaKeyReleased

private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
     int row = tbVenda.getSelectedRow();
    
    DefaultTableModel tableModel = (DefaultTableModel)tbVenda.getModel();  
    Venda v = new Venda(Integer.parseInt(tableModel.getValueAt(row, 0).toString()));
    
    if(JOptionPane.showConfirmDialog(null, "AO DELETAR A VENDA TODOS OS ITENS RELACIONADOS A ELA SERÃO EXCLUÍDOS. Tem certeza que deseja remover a venda?","ATENÇÃO!",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
        vDAO.excluir(v);
        limpaDados();
        preencheDados();
        VendaEstaSelecionada(false);
    }
    
    // TODO add your handling code here:
}//GEN-LAST:event_btnExcluirActionPerformed

private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
    preencheDados();
    VendaEstaSelecionada(false);
}//GEN-LAST:event_formWindowActivated

private void txtDataFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDataFocusLost
    ValidaData();
}//GEN-LAST:event_txtDataFocusLost

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    txtCli.setText(Cli.getNome());
}//GEN-LAST:event_formWindowGainedFocus

private void txtDataEntregaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDataEntregaFocusLost
// TODO add your handling code here:
}//GEN-LAST:event_txtDataEntregaFocusLost

private void btnNotaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNotaActionPerformed

    objetoImpressao nota = new objetoImpressao();
    Date data = new Date();  
    SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat formatadorhora = new SimpleDateFormat("HH:mm");
    ItemVendaDAO itemDAO = new ItemVendaDAO();
    Venda pai = new Venda();
       
    
    
    int row = tbVenda.getSelectedRow();
    if (row != -1){
    DefaultTableModel tableModel = (DefaultTableModel)tbVenda.getModel();
    
    nota.numNota = tableModel.getValueAt(row,0).toString();
    nota.nome = (tableModel.getValueAt(row, 1).toString());
    nota.saida = (tableModel.getValueAt(row,2).toString());
    nota.emissao = formatador.format(data);
    nota.hora = formatadorhora.format(data); 
    
    nota.prods = new ArrayList<produtosNota>();
    
    nota.valorIPI = 0;
    nota.valorTotal = 0;
    
    pai.setId(Integer.parseInt(tableModel.getValueAt(row,0).toString()));
    
    produtosNota auxiliar;
    
    try{
     ArrayList<ItensVenda> lista = itemDAO.consultar(pai);

     for(Iterator<ItensVenda> it = lista.iterator(); it.hasNext();){
         auxiliar = new produtosNota();
         
        ItensVenda i = (ItensVenda)it.next();
        auxiliar.ICMS = new Double(7);
        auxiliar.IPI = new Double(i.getProduto().getIpi());
        auxiliar.codProduto = i.getProduto().getId().toString();
        auxiliar.descricao = i.getProduto().getDescricao();
        auxiliar.qtde = i.getQtdade();
        auxiliar.valor = new Double(i.getProduto().getValorVenda());
        auxiliar.valorTotal = auxiliar.qtde*auxiliar.valor;
        auxiliar.valorIPI = auxiliar.valorTotal*auxiliar.IPI/100;
        
        nota.valorIPI += auxiliar.valorIPI;
        nota.valorTotal += auxiliar.valorTotal;
        
        nota.prods.add(auxiliar);
        
    }
      nota.baseICMS = nota.valorTotal;
     nota.valorICMS = new Double(nota.valorTotal*7/100);
     
    GeraNotaPDF.GeraNota(nota);
    
    }catch(Exception e){
        System.out.println(e.toString());
    }  
    
    }
}//GEN-LAST:event_btnNotaActionPerformed

private void VendaEstaSelecionada(boolean travar){
    txtData.setEditable(!travar);
    btnCli.setEnabled(!travar);
    txtCli.setEditable(!travar);
    btnSalvar.setEnabled(!travar);
    btnitens.setEnabled(travar);
    btnExcluir.setEnabled(travar);
}

private void limpaDados(){
    txtCli.setText("");
    txtConsulta.setText("");
    txtData.setText("");
    txtid.setText("");
}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FormVenda().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCli;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnNota;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JButton btnitens;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbVenda;
    private javax.swing.JTextField txtCli;
    private javax.swing.JTextField txtConsulta;
    private javax.swing.JFormattedTextField txtData;
    private javax.swing.JFormattedTextField txtDataEntrega;
    private javax.swing.JTextField txtValor;
    private javax.swing.JTextField txtid;
    // End of variables declaration//GEN-END:variables
}
